//
//  XNSupplierViewController.m
//  XINNingOA
//
//  Created by samuel on 2022/2/14.
//

#import "XNSupplierViewController.h"
#import "XNSupplierBillInfoRequest.h"
#import "XNSupplierApproveRequest.h"

@interface XNSupplierViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNSupplierBillInfoModel *infoModel;

@end

@implementation XNSupplierViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNSupplierBillInfoRequest *infoRequest = [[XNSupplierBillInfoRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNSupplierBillInfoRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        dispatch_group_leave(group);
    } failure:^(__kindof XNSupplierBillInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    XNSupplierApproveRequest *approveRequest = [[XNSupplierApproveRequest alloc] initWithOrderId:self.orderId];
    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNSupplierApproveRequest * _Nonnull request) {
        weakSelf.approveArray = request.approveFlowArray;
        dispatch_group_leave(group);
    } failure:^(__kindof XNSupplierApproveRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, queue, ^{
        [weakSelf creatTableData];
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}

- (void)creatTableData
{
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"单据信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    NSArray *showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"供应商名称： %@",self.infoModel.partnersName],
                              [NSString stringWithFormat:@"社会信用代码： %@",self.infoModel.partnersCode],
                              [NSString stringWithFormat:@"开户行： %@",self.infoModel.accountBank],
                              [NSString stringWithFormat:@"银行账号： %@",self.infoModel.accountNo],
                              [NSString stringWithFormat:@"公司电话： %@",self.infoModel.contactPhone],
                              [NSString stringWithFormat:@"地址： %@",self.infoModel.partnersAddress],
                              [NSString stringWithFormat:@"联系人： %@",self.infoModel.contactName],
                              [NSString stringWithFormat:@"评价： %@",self.infoModel.evaluate],
                              [NSString stringWithFormat:@"备注： %@",self.infoModel.remark.length > 0 ? self.infoModel.remark : @""],
                              [NSString stringWithFormat:@"主营内容： %@",self.infoModel.mainContents],
                              [NSString stringWithFormat:@"资质证明： "],
                              nil];
    itemModel.showItemArray = showItemArray;
    itemModel.fileArray = self.infoModel.fileList;
    itemModel.fileIsImage = YES;
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    
    XNTableShowDataModel *approveModel = [[XNTableShowDataModel alloc] init];
    approveModel.showType = XNShowType_Approve;
    approveModel.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModel];
}


@end
